package megadrum.gui;

import megadrum.*;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import megadrum.util.Log;

public class ApplicationLog extends JFrame implements ItemListener {

    public ApplicationLog() {
        this(null);
    }

    public ApplicationLog(MegadrumConfigTool mct) {
        initComponents();
        setIconImage((new ImageIcon(getClass().getResource("/megadrum/icons/icon.png"))).getImage());

        parent = mct;
        info = true;
        warning = true;
        error = true;
        log = new Log();
        MAX = -1;
        counter = 0;

        infoB.addItemListener(this);
        warnB.addItemListener(this);
        erroB.addItemListener(this);
    }

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        scroll = new javax.swing.JScrollPane();
        text = new javax.swing.JEditorPane();
        jLabel1 = new javax.swing.JLabel();
        infoB = new javax.swing.JCheckBox();
        warnB = new javax.swing.JCheckBox();
        erroB = new javax.swing.JCheckBox();

        setTitle("Application Log");
        scroll.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scroll.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        text.setBackground(new java.awt.Color(50, 50, 50));
        text.setEditable(false);
        text.setContentType("text/html");
        scroll.setViewportView(text);

        jLabel1.setText("Events in this session");

        infoB.setSelected(true);
        infoB.setText("Information");
        infoB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        infoB.setMargin(new java.awt.Insets(0, 0, 0, 0));

        warnB.setSelected(true);
        warnB.setText("Warnings");
        warnB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        warnB.setMargin(new java.awt.Insets(0, 0, 0, 0));

        erroB.setSelected(true);
        erroB.setText("Errors");
        erroB.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        erroB.setMargin(new java.awt.Insets(0, 0, 0, 0));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, scroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 532, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 237, Short.MAX_VALUE)
                        .add(infoB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(warnB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(erroB)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(erroB)
                    .add(warnB)
                    .add(infoB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(scroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    public ApplicationLog(MegadrumConfigTool mct, int max_messages) {
        this(mct);
        MAX = max_messages;
    }

    public void setParent(MegadrumConfigTool mct) {
        parent = mct;
    }

    public void itemStateChanged(ItemEvent ev) {
        if (ev.getItem() == infoB) {
            if (infoB.isSelected()) {
                info = true;
            } else {
                info = false;
            }
        } else if (ev.getItem() == warnB) {
            if (warnB.isSelected()) {
                warning = true;
            } else {
                warning = false;
            }
        } else if (ev.getItem() == erroB) {
            if (erroB.isSelected()) {
                error = true;
            } else {
                error = false;
            }
        }
        updateGUI();
    }

    public void limpiar() {
        log.clean();
        updateGUI();
    }

    public void info(String mensaje) {
        if ((++counter) == MAX) {
            log.clean();
            counter = 0;
        }
        log.info(mensaje);
        updateGUI();
    }

    public void warn(String mensaje) {
        if ((++counter) == MAX) {
            log.clean();
            counter = 0;
        }
        log.warn(mensaje);
        updateGUI();
    }

    public void error(String mensaje) {
        if ((++counter) == MAX) {
            log.clean();
            counter = 0;
        }
        log.error(mensaje);
        updateGUI();
    }

    public String getLog() {
        return "<html><body bgcolor=#323232>" + log.toString(true, true, true) + "</body></html>";
    }

    private void updateGUI() {
        text.setText("<html>" + log.toString(info, warning, error) + "</html>");
        text.setCaretPosition(text.getDocument().getLength());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox erroB;
    private javax.swing.JCheckBox infoB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane scroll;
    private javax.swing.JEditorPane text;
    private javax.swing.JCheckBox warnB;
    // End of variables declaration//GEN-END:variables
    private MegadrumConfigTool parent;
    private Log log;
    private boolean info,  warning,  error;
    private int MAX;
    private int counter;
}
